package Ajax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class TrainPage extends AbsPersonalPage {

	public TrainPage(String id, HttpServletRequest request) {
		super(id, request);
		mStrSubClassName = "resumetrain";
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {
		if (bCreate) {
			return;
		}

		if (bSubmit) {
			String address = request.getParameter("address");
			String certificate = request.getParameter("certificate");
			String traintext = request.getParameter("traintext");

			String sql = "INSERT INTO train VALUES(null, " + mResumeId + ", "
					+ (request.getParameter("property") == null ? "false" : "true") + ", "
					+ request.getParameter("dsyear") + ", "
					+ request.getParameter("dsmonth") + ", "
					+ request.getParameter("deyear") + ", "
					+ request.getParameter("demonth") + ", '"
					+ request.getParameter("organize") + "', '"
					+ request.getParameter("address") + "', '"
					+ request.getParameter("lesson") + "', '"
					+ request.getParameter("certificate") + "', '"
					+ request.getParameter("traintext") + "')";

			if (nModify > 0) {
				sql = "UPDATE train SET "
					+ "train_prop = " + (request.getParameter("property") == null ? "false" : "true")
					+ ", syear = " + request.getParameter("dsyear")
					+ ", smonth = " + request.getParameter("dsmonth")
					+ ", eyear = " + request.getParameter("deyear")
					+ ", emonth = " + request.getParameter("demonth")
					+ ", organize = '" + request.getParameter("organize");

				if (address != null && address.length() > 1) {
					sql += "', address = '" + address;
				}
				if (certificate != null && certificate.length() > 1) {
					sql += "', certificate = '" + certificate;
				}
				if (traintext != null && traintext.length() > 1) {
					sql += "', traintext = '" + traintext;
				}

				sql = sql + "', lesson = '" + request.getParameter("lesson")
						  + "' WHERE id = " + nModify;
			}

			stmt.executeUpdate(sql);

			return;
		}

		if (bDelete) {
			String sql = "DELETE FROM train WHERE id = " + mId;
			
			stmt.executeUpdate(sql);

			return;
		}

		String sql = "SELECT * FROM train WHERE ";

		if (nModify > 0) {
			sql += "id = " + nModify;
		} else {
			sql += "resume_id = " + mResumeId;
		}

		rs = stmt.executeQuery(sql);
	}

	@Override
	protected void writeWithoutCreate(PrintWriter writer) throws SQLException {
		if (rs == null) {
			return;
		}

		writer.print("<ol><li>属性: 公开, 您的简历可以被全部企业用户查看</li><li>属性: 隐藏, 企业用户不可以查看您的简历详情</li>"
				+ "</ol>"
				+ "<div class=\"topic_list\">"
				+ "<table cellpadding=\"0\" cellspacing=\"0\">"
				+ "<thead>"
				+ "<tr><td>属性</td><td>培训时间</td><td>培训课程</td><td>管理选项</td></tr>"
				+ "</thead>");

		int i = 0;

		while (rs.next()) {
			boolean is_public = rs.getBoolean("train_prop");

			writer.println("<tr class=\"bg" + (++i % 2) + "\">");
			writer.println("<td><img src=\"./images/personal/property_"
					+ (is_public ? "y" : "n") + ".gif\" alt=\"属性: "
					+ (is_public ? "公开" : "隐藏") + "\" /></td>");
			writer.println("<td>从 " + rs.getString("syear") + "-"
					+ rs.getString("smonth") + " 到 " + rs.getString("eyear")
					+ "-" + rs.getString("emonth") + "</td>");
			writer.println("<td>" + rs.getString("lesson") + "</td>");
			writer.println("<td><a href=\"#\" onclick=\"resumetrain('','','"
					+ rs.getString("id") + "');return false;\">修改</a> | ");
			writer.println("<a href=\"#\" onclick=\"if (confirm('您是否确定要删除该条教育经历\t')){resumetrain('','','','"
					+ rs.getString("id")
					+ "');return false;}else{return false;}\">删除</a></td>");
		}

		writer.println("</tr></table></div><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\"><tr><th width=\"100\">&nbsp;</th>	<td>");
		writer.println("<input type=\"button\" name=\"create\" value=\"添加培训经历\" onclick=\"resumetrain('',true)\" class=\"submit\" />");
		writer.println("<input type=\"button\" name=\"nextstep\" value=\"跳到下一步\" onclick=\"resumeload('experience')\" class=\"button\" />");
		writer.println("</td></tr></table><div id=\"resumextentform\"></div>");
	}

	@Override
	protected void writeWithCreate(PrintWriter writer) throws IOException {
		String form_title = "<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=train&amp;info=" + mResumeId + "&amp;submit=1\" ";
		File file = new File("c:\\xml\\train.xml");

		if (file.exists()) {
			InputStream fs = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fs,
					"utf-8"));

			String Line = null;

			writer.print(form_title);

			while ((Line = br.readLine()) != null) {
				writer.println(Line);
			}

			br.close();
			fs.close();
		} else {
			file.createNewFile();
		}
	}

	@Override
	protected void writeModify(PrintWriter writer) throws SQLException {
		if (rs == null || !rs.next()) {
			return;
		}

		boolean is_public = rs.getBoolean("train_prop");

		writer.print("<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=train&amp;info=" + mResumeId + "&amp;submit=1\" ");
		writer.print("name=\"wane_resume\" method=\"post\" target=\"ajaxform\" onsubmit=\"return resumetrain();\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\" style=\"margin-botton:30px;\">");
		writer.print("<caption><h2>培训经历 - 修改 - " + rs.getString("lesson") + "</h2></caption><tr><th width=\"100\"><span class=\"warn\">*</span> 培训时间:</th><td>");

		writer.print("从<a href=\"#\" onclick=\"category.load('tsyear','stimey',document.wane_resume.syear.value);return false;\" class=\"select\"><span id=\"tsyear\">" + rs.getInt("syear") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"syear\" name=\"syear\" value=\"" + rs.getInt("syear") + "\" /><input type=\"hidden\" id=\"dsyear\" name=\"dsyear\" value=\"" + rs.getInt("syear") + "\" />");
		writer.print("<a href=\"#\" onclick=\"category.load('tsmonth','stimem',document.wane_resume.smonth.value);return false;\" class=\"select\"><span id=\"tsmonth\">" + rs.getInt("smonth") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"smonth\" name=\"smonth\" value=\"" + rs.getInt("smonth") + "\" /><input type=\"hidden\" id=\"dsmonth\" name=\"dsmonth\" value=\"" + rs.getInt("smonth") + "\" />到");
		writer.print("<a href=\"#\" onclick=\"category.load('teyear','etimey',document.wane_resume.eyear.value);return false;\" class=\"select\"><span id=\"teyear\">" + rs.getInt("eyear") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"eyear\" name=\"eyear\" value=\"" + rs.getInt("eyear") + "\" /><input type=\"hidden\" id=\"deyear\" name=\"deyear\" value=\"" + rs.getInt("eyear") + "\" />");
		writer.print("<a href=\"#\" onclick=\"category.load('temonth','etimem',document.wane_resume.emonth.value);return false;\" class=\"select\"><span id=\"temonth\">" + rs.getInt("emonth") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"emonth\" name=\"emonth\" value=\"" + rs.getInt("emonth") + "\" /><input type=\"hidden\" id=\"demonth\" name=\"demonth\" value=\"" + rs.getInt("emonth") + "\" /> 至今请留空");
		writer.print("</td></tr><tr><th><span class=\"warn\">*</span> 属性:</th>	<td>");

		writer.print("<input type=\"checkbox\" name=\"property\" value=\"1\" ");

		if (is_public) {
			writer.print("checked");
		}

		writer.print(" /> 属性: 公开</td></tr><tr><th><span class=\"warn\">*</span> 培训机构:</th><td>");

		writer.print("<input type=\"text\" name=\"organize\" class=\"t_input\" size=\"35\" value=\"" + rs.getString("organize") + "\" /></td></tr><tr><th>培训地点:</th><td>");
		writer.print("<input type=\"text\" name=\"address\" class=\"t_input\" size=\"35\" value=\"" + rs.getString("address") + "\" /></td></tr><tr><th><span class=\"warn\">*</span> 培训课程:</th><td>");
		writer.print("<input type=\"text\" name=\"lesson\" class=\"t_input\" size=\"35\" value=\"" + rs.getString("lesson") + "\" /></td></tr><tr><th>所获证书:</th><td>");
		writer.print("<input type=\"text\" name=\"certificate\" class=\"t_input\" size=\"35\" value=\"" + rs.getString("certificate") + "\" /></td></tr><tr><th>培训内容:</th><td>");
		writer.print("<textarea name=\"traintext\" cols=\"85\" rows=\"6\">" + rs.getString("traintext") + "</textarea></td></tr>");
		writer.print("<tr><th>&nbsp;</th><td>");

		writer.print("<input type=\"hidden\" name=\"modifysubmit\" value=\"1\" />");
		writer.print("<input type=\"hidden\" name=\"modifid\" value=\"" + nModify + "\">");
		writer.print("<input type=\"submit\" name=\"Submit\" value=\"保存修改\" class=\"submit\" /><input type=\"button\" name=\"ClickBack\" value=\"取消操作\" onclick=\"resumeload('train');\" class=\"button\" /></td></tr></table></form>");
	}
}
